/**
 * Created with IntelliJ IDEA.
 * Description:
 * 给你单链表的头节点head，反转链表
 * User: xwl
 */


//Definition for singly-linked list.
class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class Test3 {
    // 实现
    public ListNode reverseList(ListNode head) {
        // 处理特殊情况
        if (head == null) {
            return null;
        }
        // 只有一个节点
        if (head.next == null) {
            return head;
        }
        // 定义
        ListNode cur = head.next;
        head.next = null;
        // 实现
        while (cur != null) {
            ListNode curNext = cur.next;
            // 头插法
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
}
